package db.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by daibin on 9/19/16.
 */
public abstract class NioBase<T> {

    /**
     * 线程池
     */
    public volatile ThreadPoolExecutor poolExecutor;

    /**
     * 包装处理器
     */
    public volatile Encoder<T> encoder;

    /**
     * 解析处理
     */
    public volatile Decoder<T> decoder;

    /**
     * 处理
     */
    public volatile Processor<T> processor;

    /**
     *
     */
    public volatile Class<? extends Framer> framerClass;


    /**
     * 构造函数
     *
     * @param poolExecutor 线程池
     * @param framerClass  协议封装器
     * @param encoder      编码器
     * @param decoder      解码器
     * @param processor    处理器
     */
    public NioBase(ThreadPoolExecutor poolExecutor,Class<? extends Framer> framerClass,Encoder<T> encoder, Decoder<T> decoder, Processor<T> processor) {
        this.poolExecutor = poolExecutor;
        this .framerClass = framerClass;
        this.encoder = encoder;
        this.decoder = decoder;
        this.processor = processor;
    }


    /**
     * 绑定接口
     *
     * @param address
     * @return
     */
    abstract public NioBase bind(InetSocketAddress address);

    /**
     * 启动
     */
    abstract public void startup() throws IOException, IllegalAccessException, InstantiationException, Exception;


    /**
     * 关闭
     */
    abstract public void shutdown() throws IOException;



}
